package o20_05.o02_动态数组;

public class o01_ArrayList<E> implements o01_List<E> {

    private int size = 0;

    private E[] eles; //存储内容

    // 默认大小 10 个 -> 2 看扩容
    private static final int DEFAULT_CAPACITY = 2;

    private static final int ELEMENT_NOT_FOUND = -1;

    public o01_ArrayList() {
        this(DEFAULT_CAPACITY);
    }


    public o01_ArrayList(int capacity) {
        // 默认10
        capacity = capacity < DEFAULT_CAPACITY ? DEFAULT_CAPACITY : capacity;
        this.eles = (E[]) new Object[capacity];
    }


    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean contains(E ele) {
        return indexOf(ele) != ELEMENT_NOT_FOUND;
    }

    @Override
    public void add(E ele) {
        add(size, ele);
    }

    @Override
    public E get(int index) {
        checkIndex(index);
        return eles[index];
    }

    private void checkIndex(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException(String.format("Index:%s, Size:%s", index, size));
        }
    }


    private void checkIndexForAdd(int index) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException(String.format("Index:%s, Size:%s", index, size));
        }
    }

    /**
     * 设置 index 元素,
     * 返回原来的元素
     * @param index
     * @param ele
     * @return
     */
    @Override
    public E set(int index, E ele) {
        checkIndex(index);
        E old = eles[index];
        eles[index] = ele;
        return old;
    }

    @Override
    public void add(int index, E ele) {
        checkIndexForAdd(index);
        ensureCapacity(size + 1);
        for (int i = size; i > index; i--) {
            eles[i] = eles[i-1];
        }
        size++;
        eles[index] = ele;
    }

    /**
     * 保证有多少容量
     * 不够就扩容
     * @param capacity
     */
    private void ensureCapacity(int capacity) {
        int oldCapacity = eles.length;
        if (oldCapacity >= capacity) {
            return;
        }
        // 旧容量 1.5 倍 >> 1  = * 0.5
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        E[] newEles = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newEles[i] = eles[i];
        }
        eles = newEles;
        System.out.println(String.format("扩容, old:%s, new:%s", oldCapacity, newCapacity));
    }

    @Override
    public E remove(int index) {
        checkIndex(index);
        E res = eles[index];
        size--;
        for (int i = index; i < size; i++) {
            eles[i] = eles[i + 1];
        }
        eles[size] = null; // 释放空间
        return res;
    }

    @Override
    public int indexOf(E ele) {
        if (ele == null) {
            for (int i = 0; i < size; i++) {
                if (eles[i] == null) return i;
            }
        } else {
            for (int i = 0; i < size; i++) {
                if (ele.equals(eles[i])) return i;
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    @Override
    public void clear() {
        for (int i = 0; i < size; i++) {
            eles[i] = null;
        }
        this.size = 0;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("size=");
        sb.append(size);
        sb.append(",[");
        for (int i = 0; i < size; i++) {
            sb.append(eles[i]).append(",");
        }
        return sb.substring(0, sb.length() - 1) + "]";
    }
}
